
<?php


if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){


$db = new Mypdo();
$immeuManager = new ImmeubleManager($db);

if(empty($_POST)){

    $listeImmeu = $immeuManager->getList();
    ?>

    <h1>Crée une location</h1>

    <form method="post" action="#" enctype="multipart/form-data">

        <p>
        <h3>Information</h3>
        <label for="type_loc">Type de location :</label><select name="type_loc" id="type_loc" required="required">
            <option></option>
            <option value="appart">Appartement</option>
            <option value="mais">Maison</option>
            <option value=chamb>Chambre</option>

        </select><br/>


        <label for="id_immeu">Habitation :</label><select name="id_immeu" id="id_immeu" required="required">
            <option></option>
            <?php foreach($listeImmeu as $key=>$value){
                ?><option <?php if(!empty($_GET['idi'])){echo $_GET['idi'] == $value->getIdImmeu()?'selected':'';} ?> value="<?php echo $value->getIdImmeu(); ?>"><?php echo $value->getNomImmeu(); ?></option><?php echo "\n";
            } ?>


        </select>
        <label for="nom_loc">Nom Location :</label><input name="nom_loc" type="text" id="nom_loc" required="required" /><br />
        <label for="montant_loc">Montant loyer :</label><input type="number" name="montant_loc" id="montant_loc" step="any" min="0" required="required" /><br />
        <label for="tva_loc">TVA loyer :</label><input type="number" name="tva_loc" id="tva_loc" step="any" min="0" required="required" /><br />
        <label for="descp_loc">Description :</label><textarea rows="4" cols="30" name="descp_loc" id="descp_loc" maxlength="255"  ></textarea>

        <label for="surface_loc">Surface :</label><input type="number" name="surface_loc" id="surface_loc" min="0" step="any" required="required" /><br />
        <label for="nb_piece_loc">Nombre de pièce :</label><input type="number" name="nb_piece_loc" id="nb_piece_loc" min="1" required="required" /><br />
        <label for="nb_chambre_loc">Nombre de chambre :</label><input type="number" name="nb_chambre_loc" id="nb_chambre_loc" min="0" required="required" /><br />
        <label for="meuble_loc">Meublé :</label><input type="checkbox" name="meuble_loc" id="meuble_loc" value="1"  /><br />
        <label for="exposition_loc">Exposition :</label><select name="exposition_loc" id="exposition_loc" required="required">
            <option></option>
            <option value="n">Nord</option>
            <option value="e">Est</option>
            <option value="o">Ouest</option>
            <option value="s">Sud</option>
        </select>

        </p>
        <p>
        <h1>Chargement de l'images principal</h1>
        <br/>



            <label for="file">Images (jpg, pdf): </label>
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
            <input type="file" name="file" id="file" required><br/>
            <br/>
        <label for="descp_photo">Description :</label><textarea rows="4" cols="30" name="descp_photo" id="descp_photo" maxlength="255"  ></textarea>


        </p>

        <p><input type="reset" value="Annuler" /><input type="submit" value="Valider" /></p>
    </form>

<?php }else{
    $loc = new Location($_POST);

    $locManager = new LocationManager($db);

    $id = $locManager->add($loc);

    $photoManager = new PhotoManager($db);

    $allowedExtsImage = array("jpg", "jpeg", 'png', "JPG", "JPEG",'PNG');
    $dossier_image = './image/upload/';
    $tailleImage = 10000000;



    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    /*
    echo $_FILES["file"]["name"]."</br>";
    echo $_FILES["file"]["type"]."</br>";
    echo $_FILES["file"]["size"]."</br>";
    echo $fichier
    echo $extension."</br>";
    */
    $taille = ((($_FILES["file"]["size"])/1024)/1024);
    //$chemin = $dossier_image . $fichier;


    if ((($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "application/pdf"))
        && ($_FILES["file"]["size"] < $tailleImage)
        && in_array($extension, $allowedExtsImage))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Chargement imposible";
            //echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {/*
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
	*/
            if (file_exists($dossier_image. $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " existe déjà. ";
            }
            else
            {

                    $id_loc = $locManager->getId($loc);
                    $id_photo = $photoManager->getNbPhoto($id_loc);

                    $id_photo = empty($id_photo)?1:$id_photo+1;
                    $info = new SplFileInfo($_FILES["file"]["name"]);
                    $fichier = $id_loc.'-'.$id_photo.'.'.$info->getExtension();

                    $photo = new Photo(array('id_photo'=>$id_photo,'id_loc'=>$id_loc,'lien_photo'=>$fichier,'descp_photo'=>$_POST['descp_photo']));

                    $photoManager->add($photo);
                    move_uploaded_file($_FILES["file"]["tmp_name"],
                        $dossier_image . $fichier);

                    echo 'Chargement Terminer';
                    //echo 'Stored in: ' . $dossier_image . $fichier;
                    header('Location: index.php?page=113&id='.$id);
            }
        }
    }
    else
    {
        echo "Fichier Invalide";
    }

}
}else{
    header('Location: index.php');
}